package com.yj;

import com.alibaba.fastjson.JSON;
import lombok.Data;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageConst;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Random;

@RestController
@RequestMapping("/stream")
@Slf4j
public class DemoController {

    @Autowired
    private MySource mySource; // <X>

    @GetMapping("/send")
    public boolean send() {
        // <1> 创建 Message
        DemoMessage message = new DemoMessage()
                .setId(new Random().nextInt())
                .setType("order")
                .setContent("hello stream!");
        // <2> 创建 Spring Message 对象
        Message<DemoMessage> springMessage = MessageBuilder.withPayload(message)
                .setHeader(MessageConst.PROPERTY_DELAY_TIME_LEVEL, "3")//设置延迟级别为 3，10 秒后消费
                .build();
        // <3> 发送消息
        boolean sendResult = mySource.output01().send(springMessage);
        log.info("[发送消息完成,id = {}, 结果 = {}]", message.getId(), sendResult);
        return sendResult;
    }

    @GetMapping("/send_tag")
    public boolean sendTag() {
        for (String tag : new String[]{"yj", "rocketmq", "stream"}) {
            // 创建 Message
            DemoMessage message = new DemoMessage()
                    .setId(new Random().nextInt())
                    .setType("order")
                    .setContent("hello stream!");
            // 创建 Spring Message 对象
            Message<DemoMessage> springMessage = MessageBuilder.withPayload(message)
                    .setHeader(MessageConst.PROPERTY_TAGS, tag) // 设置Tag
                    .build();
            // 发送消息
            boolean sendResult = mySource.output01().send(springMessage);
            log.info("[发送消息完成,id = {}, 结果 = {}]", message.getId(), sendResult);
        }
        return true;
    }

}